package onebymany;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import util.MybatisUtil;

public class StudGradeDao {
	/**
	 * 查询java班有哪些学生
	 */
	public List<Stud> findByGrade(String name) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			return sqlSession.selectList("studNamespace.findByGrade",name);
		} catch (Exception e) {
			e.printStackTrace();		
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
		
	}
	
	/**
	 * 查询哈哈属于哪个班级
	 */
	public Grade findByName(String name) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			return sqlSession.selectOne("gradeNamespace.findByName",name);
		} catch (Exception e) {
			e.printStackTrace();		
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
		
	}
	

	
	/**
	 * 测试
	 */
	
	public static void main(String[] args) throws Exception{
		StudGradeDao dao=new StudGradeDao();
		List<Stud> studlist=dao.findByGrade("java");
		System.out.println("java学科有"+studlist.size()+"个学生，他们分别是：");
		for (Stud s : studlist) {
			System.out.println(s.getId()+":"+s.getName());
		}
		Grade grade=dao.findByName("哈哈");
		System.out.println("哈哈是"+grade.getName()+"班的学生");
	}

}
